package com.leizi.igym.config;

import com.leizi.igym.interceptor.AdminJwtInterceptor;
import com.leizi.igym.interceptor.UserJwtInterceptor;
import com.leizi.igym.json.JacksonObjectMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @Author: leizi0110
 * @CreateTime: 2024-03-30  22:03
 * @Description:
 */
@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private AdminJwtInterceptor adminJwtInterceptor;

    @Resource
    private UserJwtInterceptor userJwtInterceptor;

    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 所有接口
                .allowCredentials(true) // 是否发送 Cookie
                .allowedOriginPatterns("*") // 支持域
                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"}) // 支持方法
                .allowedHeaders("*")
                .exposedHeaders("*");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("----------开始注册自定义拦截器----------");
        registry.addInterceptor(adminJwtInterceptor)
                // .addPathPatterns("/admin/user/**")
                // .addPathPatterns("/admin/employee/**")
                .addPathPatterns("/admin/**")
                .addPathPatterns("/file/*")
                .addPathPatterns("/workspace/**")
                .excludePathPatterns("/admin/user/login");
        registry.addInterceptor(userJwtInterceptor)
                .addPathPatterns("/user/**")
                .addPathPatterns("/file/user/**")
                .excludePathPatterns("/user/user/login","/user/user/register","/user/trainer","/user/region/**", "/user/equip","/user/category/**","/user/user/wx","/user/user/validateUsername");
    }

    /**
     * 扩展的spring mvc消息转化器,用来处理返回得LocalDateTime值
     *
     * @param: [converters]
     * @return: void
     **/
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("----------扩展消息转换器----------");
        MappingJackson2HttpMessageConverter converter =
                new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(new JacksonObjectMapper());
        converters.add(converters.size() - 1, converter);
    }


    // /**
    //  * 设置静态资源映射
    //  *
    //  * @param registry
    //  */
    // @Override
    // public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //     registry.addResourceHandler("/doc.html").addResourceLocations("classpath*:/META-INF/resources/");
    //     registry.addResourceHandler("/webjars/**").addResourceLocations("classpath*:/META-INF/resources/webjars/");
    // }
}
